#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# TwiTalker - The Twitter Gtalk BOT
# Copyright 2009-2010 Kavin Gray
# See LICENSE for details.
#
#

from google.appengine.api import xmpp
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from apptwitter import AppEngineTwitter
from db_model import TwiDbA, TwiDbB, TwiDbC, TwiDbD, TwiDbE, TwiDbF, TwiDbG, TwiDbH, TwiDbI, TwiDbJ, TwiCount,TwiError
from twi_service import TService


class XMPPHandler(webapp.RequestHandler):   
    def post(self):      
        message = xmpp.Message(self.request.POST)
        msg = message.body.strip(' ')               # get the msg
        add=message.sender.split("/")[0].lower()    # get the sender email address
        tservice=TService()
        if(tservice.isCommand(msg)):         
            respon=tservice.parseCommand(msg,add)
            message.reply(respon)
        else:
            respon=tservice.sentMsg(msg.encode('utf8'),add)
            if(respon<>'200'):
                message.reply(respon)
    

application = webapp.WSGIApplication([('/_ah/xmpp/message/chat/', XMPPHandler)],
                                     debug=True)


def main():
  run_wsgi_app(application)


if __name__ == "__main__":
  main()
